package com.tzg157.fitness.config;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaStreamingContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Configuration
public class SparkConfig {

    @Bean
    public static SparkConf localSparkConf() {
        SparkConf conf = new SparkConf();
        conf.setMaster("local[*]");
        conf.setAppName("fitness");
        return conf;
    }

    @Bean
    public ConcurrentMap<String,Object> queueMap(){
        return new ConcurrentHashMap<>();
    }

    @Bean
    public JavaStreamingContext localStreamingContext(){
        return new JavaStreamingContext(localSparkConf(), Durations.seconds(1));
    }

    @Bean
    public SparkSession localSparkSession(){
        return SparkSession.builder()
                .config(localStreamingContext().sparkContext().getConf())
                .enableHiveSupport()
                .config("hive.metastore.uris", "thrift://localhost:9083")
                .getOrCreate();
    }
}
